package com.ns.school.controller.edu;

import com.github.pagehelper.PageHelper;
import com.ns.school.common.config.SecurityUtil;
import com.ns.school.common.utils.CommonUtil;
import com.ns.school.common.utils.DateUtil;
import com.ns.school.common.utils.DecimalUtil;
import com.ns.school.common.utils.JsonUtil;
import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.BookStockDB;
import com.ns.school.po.edu.BorrowBookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.service.edu.BookService;
import com.ns.school.service.edu.BorrowBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Controller
@Slf4j
public class BorrowBookController {

    @Autowired
    private BorrowBookService borrowBookService;
    @Autowired
    private BookService bookService;

    @RequestMapping("/borrow/list")
    public String list(Model model) {
        return "view/bookBorrow/list";
    }

    @RequestMapping("/borrow/add")
    public String add(Model model) {
        return "view/bookBorrow/add";
    }

    @RequestMapping("/bookBorrow/revertPrice")
    public String edit(Model model, Integer id, ModelMap modelMap) {
        BorrowBookDB borrowBookDB =borrowBookService.editBorrowBook(id);// 此处需要通过参数id获取到具体的图书对象
        modelMap.put("bookBorrow",borrowBookDB);
        return "view/bookBorrow/revertPrice";
    }

    //借书查询
    //获取借书列表
    @RequestMapping("/findBorrowBook")
    @ResponseBody
    public Object findBorrowBook(BorrowBookDB borrowBookDB, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<BorrowBookDB> listAll = borrowBookService.findBorrowBook(borrowBookDB);
        log.info(JsonUtil.toStr(listAll));
        return CommonUtil.getStringObjectMap(listAll);

    }

    //借阅

    @RequestMapping("/borrowBook")
    @ResponseBody
    public LayuiResult<BorrowBookDB> borrowBook(@RequestBody BorrowBookDB borrowBookDB) {
        LayuiResult<BorrowBookDB> result = new LayuiResult<>();
        borrowBookService.borrowBook(result,borrowBookDB);
        return result;
    }



    @RequestMapping("/auditBorrow")
    @ResponseBody
    public LayuiResult<BorrowBookDB> auditBorrow(BorrowBookDB borrowBookDB) {
        Integer reviewId = SecurityUtil.id();
        LayuiResult result = new LayuiResult<>();
        int count = borrowBookService.checkBorrow(borrowBookDB);
        if (count > 0) {
            result.setCode(500);
            result.setMsg("该审核记录已处理，请勿重复审核。");
            return result;
        }
        BorrowBookDB bb = borrowBookService.findBorrowById(borrowBookDB.getBorrowId());
        BookDB check = bookService.selectByBookId(bb.getBookId());
        int a = check.getBookState();
        int bqy = check.getBookQty();
        int borrowCount = bb.getBorrowCount();
        if (a == 0) {
            result.setCode(500);
            result.setMsg("图书已下架，不可借阅");
            return result;
        }
        if (bqy - borrowCount < 0) {
            result.setCode(500);
            result.setMsg("库存不足");
            return result;
        } else {
            // 执行审核操作
            borrowBookDB.setReviewId(reviewId);
            borrowBookService.auditBorrow(result, borrowBookDB);
            return result;
        }
    }


        //审核拒绝
        @RequestMapping("auditBorrowFail")
        @ResponseBody
        public LayuiResult<BookStockDB> auditFailBooks (BorrowBookDB borrowBookDB){
            Integer reviewId = SecurityUtil.id();
            LayuiResult result = new LayuiResult();
            int count = borrowBookService.checkBorrow(borrowBookDB);
            if (count > 0) {
                result.setCode(500);
                result.setMsg("该审核记录已处理，请勿重复审核。");
            } else {
                borrowBookDB.setReviewId(reviewId);
                borrowBookService.auditFailBooks(result, borrowBookDB);
            }

            return result;
        }


        //图书归还
    @RequestMapping("revertBorrow")
    @ResponseBody
    public LayuiResult<BookStockDB> revertBooks (@RequestBody BorrowBookDB borrowBookDB){
        borrowBookDB.setBorrowId(borrowBookDB.getId());
        LayuiResult result = new LayuiResult<>();
        // 获取当前日期和时间
        Date currentDate = new Date();
        borrowBookDB.setRevertTime(currentDate);

        BorrowBookDB bp=borrowBookService.findReviewById(borrowBookDB.getBorrowId());
        borrowBookDB.setBorrowDate(bp.getBorrowDate());
        log.info(JsonUtil.toStr(bp.getBorrowDate()));
        log.info(JsonUtil.toStr(borrowBookDB.getRevertTime()));

        borrowBookDB.setRevertDay(DateUtil.betweenDayC(borrowBookDB.getBorrowDate(),borrowBookDB.getRevertTime()));

        int br= borrowBookDB.getBorrowCount()-borrowBookDB.getRevertCount();

        if (br<0){
            result.setCode(500);
            result.setMsg("归还数量有错，超过借阅数量");
            return result;
        }else{
            borrowBookDB.setCompenPrice(DecimalUtil.mulB(BigDecimal.valueOf(br),borrowBookDB.getBookPrice()));
            int p=borrowBookService.revertBook(borrowBookDB);
            if(p>0){
                result.setCode(0);
                result.setMsg("归还成功");

            }
            return result;
        }



    }


}
